package com.qeeyou.apps.android.craftsman.activity.order;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.po.CommonInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.HashMap;
import java.util.Map;

public class CancelOrderActivity extends Activity {


    private Button btnConfirm;
    private Button btnCancel;

    private CustomLoadingDialog mLoadingDialog;
    private String mOrderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cancel_order);
        getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        Intent intent = getIntent();
        if(intent != null && intent.getStringExtra("orderId") != null) {
            mOrderId = intent.getStringExtra("orderId");
        }
        setView();
        setEvent();
    }

    private void setView() {

        btnConfirm = (Button) this.findViewById(R.id.btnConfirm4CancelOrder);
        btnCancel = (Button) this.findViewById(R.id.btnCancel4CancelOrder);
    }

    private void setEvent() {
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CancelOrderActivity.this.finish();
            }
        });

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(mOrderId != null) {
                   String[] params = new String[] {mOrderId};
                   new CancelOrderAction().execute(params);
               }
            }
        });

    }

    //取消订单
    class CancelOrderAction extends AsyncTask<String, Void, CommonInfo> {

        public CancelOrderAction() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(CancelOrderActivity.this);
            }
            mLoadingDialog.setMessage("正在取消订单...");
            mLoadingDialog.show();
        }

        @Override
        protected CommonInfo doInBackground(String... params) {
            String orderId = params[0];
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("orderid", orderId);
            CommonInfo orderInfo = AppService.getInstance().cancelOrderAction(paramMap);
            return orderInfo;
        }

        @Override
        protected void onPostExecute(CommonInfo resultInfo) {
            super.onPostExecute(resultInfo);
            if(mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if(resultInfo != null) {
                if(resultInfo.getStatus() != null && resultInfo.getStatus().trim().equalsIgnoreCase("1")) {
                    Toast.makeText(CancelOrderActivity.this, "取消订单成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(CancelOrderActivity.this, "取消订单失败", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(CancelOrderActivity.this, "取消订单失败", Toast.LENGTH_SHORT).show();
            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent();
                    CancelOrderActivity.this.setResult(500, intent);
                    CancelOrderActivity.this.finish();
                }
            }, 800);
        }
    }

}
